#pragma once

//
// it is necessary to include "U7Objs.h" before including this header file,
// because U7Objs.h includes U7FileDefs.h.
//

class WorldMap;

class MapChunk
{
	friend class Renderer;

	// no replicated
	MapChunk( const MapChunk& x );
	MapChunk& operator = ( const MapChunk& x );

private:
	WorldMap*		worldMap;
	OBJLIST			objects;
	OBJITR			firstNonFlat;
	unsigned char	xchunk; // 0 - 191, 0xFF: not owned by a map
	unsigned char	ychunk; // 0 - 191, 0xFF: not owned by a map

	PATTERN_ID		patternID;

	unsigned		refFromBelow;
	unsigned		refFromRight;
	unsigned		refFromBelowRight;

	void ResetTerrain();
	void UpdateDependencies( BasicObject* obj );

public:
	MapChunk( WorldMap* map, int chx, int chy );
	~MapChunk();

	void GetChunkCoords( U7VECTOR2* coords ) const;
	void GetTileCoords( U7VECTOR2* coords ) const;

	void SetTerrain(PATTERN_ID pid);
	void SetIfixObjects( int count, const IFIXENTRY* entry );

	void Add( BasicObject* newObj, int x, int y, int z );
	bool Remove( BasicObject* removedObj );
	BasicObject* Find( int xt, int yt, int zlevel, const SHAPEID& shapeID ) const;

	void HitTest( int worldX, int worldY, int zlimit, OBJLIST& hits ) const;
};


inline void MapChunk::UpdateDependencies( BasicObject* obj )
{
	obj->AddDependencies( firstNonFlat, objects.end() );
}

inline void MapChunk::GetChunkCoords( U7VECTOR2* coords ) const
{
	coords->x = xchunk;
	coords->y = ychunk;
}

inline void MapChunk::GetTileCoords( U7VECTOR2* coords ) const
{
	coords->x = xchunk * TILES_PER_CHUNK_IN_WIDTH;
	coords->y = ychunk * TILES_PER_CHUNK_IN_HEIGHT;
}

